package com.csf.service.impl;

import com.csf.dao.DiscussPostMapper;
import com.csf.entity.DiscussPost;
import com.csf.service.DiscussPostService;
import com.csf.service.UserService;
import com.csf.util.MGWordFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
public class DiscussPostServiceImpl implements DiscussPostService {

    @Autowired
    private DiscussPostMapper discussPostMapper;

    @Autowired
    private MGWordFilter mgWordFilter;

    @Override
    public List<DiscussPost> findDiscussPost(int userId, int offset, int limit,int orderMode) {
        List<DiscussPost> discussPost = discussPostMapper.findDiscussPost(userId, offset, limit,orderMode);
        return discussPost;
    }

    @Override
    public int DiscussPostRows(int userId) {
        int i = discussPostMapper.DiscussPostRows(userId);
        return i;
    }

    @Override
    public int insertDiscussPost(DiscussPost discussPost) {

        String content = discussPost.getContent();
        String title = discussPost.getTitle();
//        springMVC提供转义HTML标记
        discussPost.setContent(HtmlUtils.htmlEscape(content));
        discussPost.setTitle(HtmlUtils.htmlEscape(title));

//        敏感词过滤
        discussPost.setContent(mgWordFilter.filter(discussPost.getContent()));
        discussPost.setTitle(mgWordFilter.filter(discussPost.getTitle()));

        return discussPostMapper.insertDiscussPost(discussPost);
    }

    @Override
    public DiscussPost selectDisById(int id) {
        return discussPostMapper.selectDisById(id);
    }

    @Override
    public int updateDisCount(int id, int commentCount) {
        return discussPostMapper.updateDisCount(id,commentCount);
    }

    @Override
    public int updateType(int type, int id) {
        return discussPostMapper.updateType(type, id);
    }

    @Override
    public int updateStatus(int status, int id) {
        return discussPostMapper.updateStatus(status, id);
    }

    @Override
    public int updateDiscussScore(int postId, double score) {
        return discussPostMapper.updateDiscussScore(postId,score);
    }
}
